package com.hxkj.controller;

import com.github.pagehelper.PageInfo;
import com.hxkj.domain.po.UserFootprint;
import com.hxkj.domain.result.UserParameterDataStruct;
import com.hxkj.service.UserFootprintService;
import com.hxkj.token.TokenGenerator;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * created by kezhijie on 2018/8/28 9:31.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("footprint")
public class UserFootprintController {

    @Autowired
    private UserFootprintService userFootprintService;

    @Autowired
    private TokenGenerator tokenGenerator;


    @PostMapping("save")
    public ResultMessageStruct save(@RequestBody UserFootprint userFootprint) {
        return userFootprintService.save(userFootprint);
    }

    @PostMapping("get")
    public ResultDataStruct<PageInfo> get(@RequestBody ParameterDataStruct<UserFootprint> parameterDataStruct) {
        ResultDataStruct<PageInfo> resultDataStruct = new ResultDataStruct<>();

        try {
            resultDataStruct.setData(userFootprintService.selectListByUserId(parameterDataStruct.getData(), parameterDataStruct.getPageTo()));
            resultDataStruct.setCode(200);
            resultDataStruct.setMessage("成功");
        } catch (Exception e) {
            resultDataStruct.setCode(500);
            resultDataStruct.setMessage("失败");
        }

        return resultDataStruct;
    }

    private String getUserId(HttpServletRequest request) {
        String token = request.getHeader("accessToken");
        return tokenGenerator.getUserIdByToken(token);
    }
}
